package common.dao.Impl;


import cn.hutool.core.util.StrUtil;
import common.dao.MajorDao;
import common.domain.Major;
import common.domain.Role;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class MajorDaoImpl implements MajorDao {

    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;
    @Override
    public void insert(Major major) {
        String sql = "insert into major\n" +
                "        (major_id,major_name,major_info,dept_id,majorType_id) \n" +
                "values(?,?,?,?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,major.getMajor_id());
            psmt.setString(2,major.getMajor_name());
            psmt.setString(3,major.getMajor_info());
            psmt.setLong(4,major.getDept_id());
            psmt.setLong(5,major.getMajorType_id());

            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(Major major) {
        String sql = "update major set";
        List<Object> param = new ArrayList<>();
        if (major == null){
            return;
        }else{
            if(major.getMajor_id() != null){
                sql += " major_id = ?,";
                param.add(major.getMajor_id());
            }
            if(major.getMajor_name() != null){
                sql += " major_name = ?,";
                param.add(major.getMajor_name());
            }
            if(major.getMajor_info() != null){
                sql += " major_info = ?,";
                param.add(major.getMajor_info());
            }
            if(major.getDept_id() != null){
                sql += " dept_id = ?,";
                param.add(major.getDept_id());
            }
            if(major.getMajorType_id() != null){
                sql += " majortype_id = ?,";
                param.add(major.getMajorType_id());
            }
        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where major_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,major.getMajor_id());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long major_id) {
        String sql = "delete from major where major_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,major_id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public Major selectById(Long major_id) {
        String sql = "select * from major where major_id = ?";
        Major major = new Major();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,major_id);
            rs = psmt.executeQuery();
            while (rs.next()){
                major.setMajor_id(rs.getLong(1));
                major.setMajor_name(rs.getString(2));
                major.setMajor_info(rs.getString(3));
                major.setDept_id(rs.getLong(4));
                major.setMajorType_id(rs.getLong(5));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return major;
    }

    @Override
    public List<Major> selectList(Major major) {
        String sql = "select * from major where 1=1";
        List<Major> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (major == null){
            sql = "select * from major";
        }else{
            if(major.getMajor_id() != null){
                sql += " and major_id = ?";
                param.add(major.getMajor_id());
            }
            if(major.getMajor_name() != null){
                sql += " and major_name like \"%\"?\"%\"";
                param.add(major.getMajor_name());
            }
            if(major.getMajor_info() != null){
                sql += " and major_info = ?";
                param.add(major.getMajor_info());
            }
            if(major.getDept_id() != null){
                sql += " and dept_id = ?";
                param.add(major.getDept_id());
            }
            if(major.getMajorType_id() != null){
                sql += " and majortype_id = ?";
                param.add(major.getMajorType_id());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()){
                Major major1 = new Major();
                major1.setMajor_id(rs.getLong(1));
                major1.setMajor_name(rs.getString(2));
                major1.setMajor_info(rs.getString(3));
                major1.setDept_id(rs.getLong(4));
                major1.setMajorType_id(rs.getLong(5));
                list.add(major1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return list ;
    }

}
